using Intelli.Cust.Svcs.Models;
using Intelli.Cust.Svcs.Services;
using Serilog;

var builder = WebApplication.CreateBuilder(args);

// 配置Serilog日志
Log.Logger = new LoggerConfiguration()
    .ReadFrom.Configuration(builder.Configuration)
    .Enrich.FromLogContext()
    .WriteTo.Console()
    .WriteTo.File("logs/intelligent-customer-service-.log", rollingInterval: RollingInterval.Day)
    .CreateLogger();

builder.Host.UseSerilog();

// Add services to the container.
builder.Services.AddControllers();

// 配置选项绑定
builder.Services.Configure<AppSettings>(
    builder.Configuration.GetSection("AppSettings"));

// 配置内存缓存
builder.Services.AddMemoryCache();

// 配置HTTP客户端
builder.Services.AddHttpClient();

// 注册智能客服服务
builder.Services.AddScoped<IIntelligentCustomerService, IntelligentCustomerService>();

// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen(c =>
{
    c.SwaggerDoc("v1", new() { Title = "智能客服系统 API", Version = "v1" });
});

var app = builder.Build();

// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
    app.UseSwagger();
    app.UseSwaggerUI(c =>
    {
        c.SwaggerEndpoint("/swagger/v1/swagger.json", "智能客服系统 API v1");
        c.RoutePrefix = string.Empty; // 设置Swagger为根路径
    });
}

app.UseAuthorization();

app.MapControllers();

// 健康检查端点
app.MapGet("/health", () => new { status = "healthy", timestamp = DateTime.UtcNow });

try
{
    Log.Information("启动智能客服系统...");
    app.Run();
}
catch (Exception ex)
{
    Log.Fatal(ex, "智能客服系统启动失败");
}
finally
{
    Log.CloseAndFlush();
}
